package com.simple.system.util;

import com.simple.system.filter.JwtFilter;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.SignatureException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.mysql.cj.conf.PropertyKey.logger;

public class JwtUtils {
    private static final String SECRET_KEY = "pro_my_secret_key";
    private static final long EXPIRATION_TIME = 864_000_000; // 10 days
    private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class);


    public static String generateToken(Long userId) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", userId);
        return Jwts.builder()
               .setClaims(claims)
               .setSubject(String.valueOf(userId))
               .setIssuedAt(new Date(System.currentTimeMillis()))
               .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
               .signWith(SignatureAlgorithm.HS512, SECRET_KEY)
               .compact();
    }




    public static Claims validateToken(String token) {
        try {
            return Jwts.parser()
                    .setSigningKey(SECRET_KEY)
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {
            logger.error("JWT 已过期: {}", e.getMessage());
            throw e;
        }
    }
}